package com.yidu.InventoryManagement.service.impl;

import com.yidu.BusinessParament.entity.Cash;
import com.yidu.InventoryManagement.Util.IdUtil;
import com.yidu.InventoryManagement.entity.CashAccrualStock;
import com.yidu.InventoryManagement.entity.Fund;
import com.yidu.InventoryManagement.mapper.CashAccrualStockMapper;
import com.yidu.InventoryManagement.service.cashAccrualStockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author z
 * @version 1.0
 * @date 2022/12/8
 * 现金应收应付库存
 */
@Service
public class cashAccrualStockServiceImpl implements cashAccrualStockService {
    @Resource
    private CashAccrualStockMapper cashAccrualStockMapper;
    /**
     * 查询分页
     * @param
     * @return
     */
    public Map selectCashAccrualStock(int limit,int page,String date,String businessType) {
        String strWhere=append(date,businessType);

        Map layUiMap=new HashMap();

        Map map=new HashMap();
      //分页
        map.put("limit",limit);
        map.put("page",(page-1)*limit);
        map.put("strWhere",strWhere);
        //layui渲染的格式 将数据转化为layui可接受的格式
        layUiMap.put("msg","");
        layUiMap.put("code",0);
        layUiMap.put("data",cashAccrualStockMapper.selectCashAccrualStock(map));
        layUiMap.put("count",cashAccrualStockMapper.selectCashAccrualStockInfo(map));

       return layUiMap;
    }
    //搜索日期跟类型的字符串拼接
    public String append(String date, String businessType){
        //创建一个stringBuffer
        StringBuffer term=new StringBuffer();
        //日期
        if(date!=null && !date.equals("")){
            term.append(" and date=").append(" '"+date+" '");
        }
        //类型
        if(businessType!=null && !businessType.equals("")){
            term.append(" and businessType=").append(businessType);
        }
        return term.toString();
    }

    public int addCashAccrualStock() {
        return 0;
    }
    //新增
    public int addCashAccrualStock(CashAccrualStock accrualStock) {
        return cashAccrualStockMapper.addCashAccrualStock(accrualStock);

    }
   //查询最大的ID
    public String getmaxcashAccrualStockId() {
        return cashAccrualStockMapper.getmaxcashAccrualStockId();
    }
//修改
    public int updateCashAccrualStock(CashAccrualStock accrualStock) {
        return cashAccrualStockMapper.updateCashAccrualStock(accrualStock);
    }

/*//删除
    public int deleteCashAccrualStock(String cashAccrualStockId) {
        return cashAccrualStockMapper.deleteCashAccrualStock(cashAccrualStockId);
    }*/
    /**
     * 删除（单个删除和多个删除）
     *
     * @param
     * @return list 集合
     */
    public int deleteCashAccrualStock(String cashAccrualStockId) {
        //将id转为数据
        String[] split = cashAccrualStockId.split(",");
        ArrayList<Object> list = new ArrayList<Object>();
        for (String id : split) {
            //将id存入集合
            list.add(id);
        }
        int i = cashAccrualStockMapper.deleteCashAccrualStock(list);
        return i;
    }









    @Override
    public int selectCashAccrualStockId() {
        return cashAccrualStockMapper.selectCashAccrualStockId();
    }



//    @Override
//    public Map selectCashIdAndCashName(int page, int limit, String cashName) {
//        return null;
//    }


    /**
     * 下拉表格的查询现金信息
     * @param page 当前页
     * @param limit 每页显示数
     * @param accountName 现金名称
     * @return list 集合
     */

    public Map selectCashIdAndAccountName(int page, int limit, String accountName) {
        Map map = new HashMap();
        map.put("page", (page-1)*limit);
        map.put("limit", limit);
        map.put("term", append2(accountName));
        List<Cash> securities =cashAccrualStockMapper.selectCashIdAndAccountName(map);

        //查行数
        int count=cashAccrualStockMapper.selectCashIdAndAccountNameCount(map);
        Map layUiMap = new HashMap();
        layUiMap.put("code", 0);
        layUiMap.put("msg", "");
        layUiMap.put("count", count);
        layUiMap.put("data", securities);
        return layUiMap;
    }


    /**
     * 下拉框现金信息，拼接条件的方法
     * @param accountName 现金名称
     * @return
     */
    private String append2(String accountName) {
        String sql = "";
        if (accountName != null && !accountName.equals("")) {
            sql += " and accountName=" + accountName;
        }
        return sql;
    }

    /**
     * 分页查询下拉表格的查询现金信息
     * @param accountName 现金名称
     * @return
     */
    public int selectCashIdAndCashAccountCount(String accountName) {
        Map map = new HashMap();
        map.put("accountName", accountName);
        int count = cashAccrualStockMapper.selectCashIdAndAccountNameCount(map);
        return count;
    }



    ///////////

    /**
     * 获得应付赎回款以及两费总和
     *
     * @param time 指定时间
     * @return double 赎回以及两费总和
     * @author 任俊杰
     */
    @Override
    public double getRedemptionOfTwoFeesSum(String time) {
        return cashAccrualStockMapper.getRedemptionOfTwoFeesSum(time);
    }


}




